package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.task.Cache;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Acomfzr;
import com.feicheng.persistence.entity.Adz;
import com.feicheng.persistence.entity.Asetting;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.service.IAcomfzrService;
import com.feicheng.persistence.service.IAdzService;
import com.feicheng.persistence.service.IClientService;
import com.feicheng.wxapi.app.WxApi;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("/wx/login/md")
@RequestMapping("/wx/login/md")
public class MdLoginController {

    @Autowired
    IAdzService iAdzService;

    @Autowired
    IAcomfzrService iAcomfzrService;

    @Autowired
    WxApi wxApi;

    @Autowired
    Cache cache;

    @Autowired
    HttpSession session;

    @GetMapping("/jscode")
    public ResultInfo jscode(String code, String fromid, String phoneCode) {
        JSONObject result = new JSONObject();

        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String openId = wxApi.getOpenId(
                code,
                asetting.getDappid(),
                asetting.getDsecretkey());

        QueryWrapper<Adz> dzQueryWrapper = new QueryWrapper<Adz>();
        dzQueryWrapper.eq("openid", openId);
        dzQueryWrapper.eq("appid", asetting.getDappid());
        List<Adz> adzs = iAdzService.list(dzQueryWrapper);

        QueryWrapper<Acomfzr> fzrQueryWrapper = new QueryWrapper<Acomfzr>();
        fzrQueryWrapper.eq("openid", openId);
        fzrQueryWrapper.eq("appid", asetting.getDappid());
        List<Acomfzr> acomfzrs = iAcomfzrService.list(fzrQueryWrapper);

        if (0 == adzs.size() && 0 == acomfzrs.size()) {
            result.put("openid", openId);
        } else {
            if (1 == adzs.size()) {
                Adz adz = adzs.remove(0);
                result.put("adz", adz);
            } else if (1 < adzs.size()) {
                result.put("adzs", adzs);
            }

            if (1 == acomfzrs.size()) {
                Acomfzr acomfzr = acomfzrs.remove(0);
                result.put("acomfzr", acomfzr);
            } else if (1 < acomfzrs.size()) {
                throw new SystemException("当前账户非法");
            }
        }

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }

    @PostMapping("/dz/bind")
    public ResultInfo bindDz(@RequestBody JSONObject param) {

        String openid = param.getString("openid");
        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        Adz adz = new Adz();
        adz.setOpenid(openid);
        adz.setDzmc("未设置店长名称");
        adz.setAppid(asetting.getDappid());
        adz.setDzid(StringGenerator.getID("DZ"));
        adz.setCover("https://wimg.588ku.com/gif620/21/04/25/26c111bdfb2b4c9e872d83f530a86e4b.gif");
        iAdzService.save(adz);
        param.put("adz", adz);

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, param);

        return ResultInfo.success(param);
    }

    @PostMapping("/fzr/bind")
    public ResultInfo bindFzr(@RequestBody JSONObject param) {

        String openid = param.getString("openid");
        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        Acomfzr acomfzr = new Acomfzr();
        acomfzr.setOpenid(openid);
        acomfzr.setAppid(asetting.getDappid());
        acomfzr.setFzrid(StringGenerator.getID("FZR"));
        acomfzr.setCover("https://wimg.588ku.com/gif620/21/04/25/26c111bdfb2b4c9e872d83f530a86e4b.gif");
        iAcomfzrService.save(acomfzr);
        param.put("acomfzr", acomfzr);

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, param);

        return ResultInfo.success(param);
    }

    @Transactional
    @PostMapping("/dz/phone")
    public ResultInfo changePhone(@RequestBody JSONObject param) {

        String dzid = param.getString("dzid");
        String phoneCode = param.getString("phoneCode");

        JSONObject result = new JSONObject();
        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String phone = wxApi.getPhoneNumber(
                "",
                asetting.getDappid(),
                asetting.getDsecretkey(),
                phoneCode);

        Adz adz = iAdzService.getById(dzid);
        adz.setTel(phone);
        iAdzService.updateById(adz);
        result.put("adz", adz);
        return ResultInfo.success(result);
    }
}
